const Koa = require('koa');

const pageRouter = require('./routers/dev-ssr');
const app = new Koa();

const isDev = process.env.NODE_ENV === 'development'

app.use(async (ctx,next) =>{//请求的中间件，记录所有的访问和返回的信息
    try{
        console.log(`require width path ${ctx.path}`);
        await next();
    }catch(err){
        console.log(err);
        ctx.status = 500
        if(isDev){
          ctx.body = err.message
        }else{
          ctx.body = 'please try again later'
        }
    }
})

app.use(pageRouter.routes()).use(pageRouter.allowedMethods())

const HOST = process.env.HOST || '0.0.0.0'
const PORT = process.env.PORT || 3333

app.listen( PORT,HOST,()=>{
    console.log(`server is listening on ${HOST}:${PORT}`)
} )